package cn.cjlmonster.demo02.utils;

import android.content.Context;
import android.content.SharedPreferences;

import cn.cjlmonster.demo02.MyApplication;

/**
 * This project is Demo02
 * Created by cjl on 2021/11/20 5:10 下午.
 */
public class SPUtils {

    private static class SingleInstance {
        static final SPUtils instance = new SPUtils();
    }

    public static SPUtils getInstance() {
        return SingleInstance.instance;
    }

    private final SharedPreferences sp;

    private SPUtils() {
        sp = MyApplication.getInstance().getSharedPreferences("demo02", Context.MODE_PRIVATE);
    }

    public void putInt(String key, int value) {
        sp.edit().putInt(key, value).commit();
    }

    public void putFloat(String key, float value) {
        sp.edit().putFloat(key, value).commit();
    }

    public void putLong(String key, long value) {
        sp.edit().putLong(key, value).commit();
    }

    public void putBoolean(String key, boolean value) {
        sp.edit().putBoolean(key, value).commit();
    }

    public void putString(String key, String value) {
        sp.edit().putString(key, value).commit();
    }

    public int getInt(String key) {
        return getInt(key, -1);
    }

    public int getInt(String key, int def) {
        return sp.getInt(key, def);
    }

    public float getFloat(String key) {
        return getFloat(key, -1);
    }

    public float getFloat(String key, float def) {
        return sp.getFloat(key, def);
    }

    public long getLong(String key) {
        return getLong(key, -1);
    }

    public long getLong(String key, long def) {
        return sp.getLong(key, def);
    }

    public boolean getBoolean(String key) {
        return getBoolean(key, false);
    }

    public boolean getBoolean(String key, boolean def) {
        return sp.getBoolean(key, def);
    }

    public String getString(String key) {
        return getString(key, null);
    }

    public String getString(String key, String def) {
        return sp.getString(key, def);
    }
}
